<!-- 修改密码  -->
<template>
	<view class="update">
		<view class="content">
			<view v-if='oldShow==2'>
				<u--input clearable placeholder="请输入旧密码" border="none" v-model="oldPwd" type="password"></u--input>
			</view>
			<view>
				<u--input clearable placeholder="请输入新密码" border="none" v-model="newPwd" type="password"></u--input>
			</view>
			<view v-if='oldShow==2'>
				<u--input clearable placeholder="请再次输入新密码" border="none" v-model="newPwd2" type="password"></u--input>
			</view>
		</view>
		<view class="btn" @click="submit">确定</view>
		<success-popup :show="show" text="密码修改成功"></success-popup>
	</view>
</template>

<script>
	import successPopup from '@/components/successPopup.vue'
	import {
		updatePwd,
		setPwd
	} from '@/apis/apis.js'
	export default {
		components: {
			successPopup
		},
		data() {
			return {
				oldPwd: '',
				newPwd: '',
				newPwd2: '',
				show: false,
				oldShow:null
			}
		},
		onLoad(options) {
			console.log(options.set);
			this.oldShow= options.set ? options.set:-1
		},
		methods: {
			submit() {
				const boo = this.validation()
				console.log(boo)
				if (boo) {
					if(this.oldShow==2){
						//修改密码
						updatePwd({
							oldPassword: this.oldPwd,
							newPassword: this.newPwd,
						}).then(res => {
							console.log(res)
							if (res.code == 200) {
								this.show = true
								setTimeout(() => {
									this.show = false
									uni.reLaunch({
										url: '/pages/login/login'
									})
								}, 1500)
							}
						})
					}
					if(this.oldShow==1){
						//设置密码
						setPwd({
							password:this.newPwd
						}).then(res=>{
							console.log(res,'设置密码');
						})
					}
					
				}
			},
			validation() {
				if (!this.oldPwd&&this.oldShow==2) {
					uni.showToast({
						title: '请填写旧密码',
						icon: 'none'
					})
					return
				}
				if (!this.newPwd) {
					uni.showToast({
						title: '请填写新密码',
						icon: 'none'
					})
					return
				}
				if (!this.newPwd2&&this.oldShow==2) {
					uni.showToast({
						title: '请填确认密码',
						icon: 'none'
					})
					return
				}
				if (this.newPwd !== this.newPwd2&&this.oldShow==2) {

					uni.showToast({
						title: '确认密码不一致',
						icon: 'none'
					})
					return
				}
				return true
			}
		}
	}
</script>

<style lang="scss" scoped>
	.update {
		// min-height: 100vh;
		background-color: #F8F8F8;
		padding: 30rpx;

		.content {
			background-color: #fff;
			box-shadow: 0px 2rpx 90rpx 0px rgba(174, 185, 190, 0.2000);
			border-radius: 20rpx;
			padding: 0 30rpx;

			&>view {
				padding: 38rpx 0rpx;
			}

			&>view:not(&>view:nth-last-of-type(2)) {
				border-bottom: 2rpx solid #F1F1F1;
			}
		}

		.btn {
			width: 636rpx;
			height: 90rpx;
			background: #194FC9;
			border-radius: 46rpx;
			text-align: center;
			line-height: 90rpx;
			font-size: 30rpx;
			font-family: Source Han Sans CN;
			font-weight: 400;
			color: #FFFFFF;
			margin: 80rpx auto 0;
		}

		.popupText {
			font-size: 32rpx;
			font-weight: 500;
			color: #434343;
			padding-top: 20rpx;
		}

	}
</style>
